
package eft.android.view;


import java.util.ArrayList;
import java.util.List;

import me.croute.calendarexample.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import eft.android.controller.Controller;
import eft.android.controller.Workout;

public class PopulateFromDB extends Activity{

	private List<Workout> workoutList;
	private Controller controller;
	private TableLayout workout_title_table;
	
	private long date;


	// Initializing stuff when first called
	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.populatefromdb);
			
			Bundle extras = getIntent().getExtras();

			if( extras.getLong("Date") != 0 ){
				date = extras.getLong("Date");
			}

			workout_title_table = (TableLayout) findViewById(R.id.workout_title_table);

			Button girlWorkouts = (Button) findViewById(R.id.girlWorkouts);
			Button heroWorkouts = (Button) findViewById(R.id.heroWorkouts);
			Button olympicWorkouts = (Button) findViewById(R.id.olympicWorkouts);
			Button userWorkouts = (Button) findViewById(R.id.userWorkouts);

			//set click listeners for all the buttons
			girlWorkouts.setOnClickListener(girlWorkoutsListener);
			heroWorkouts.setOnClickListener(heroWorkoutsListener);
			olympicWorkouts.setOnClickListener(olympicWorkoutsListener);
			userWorkouts.setOnClickListener(userWorkoutsListener);

			controller = Controller.getInstance();

		}
		catch (Exception e) {
			Log.e("ERROR", "ERROR IN CODE: " + e.toString());
			e.printStackTrace();
		}

	}


	// Create click listeners for buttons
	private View.OnClickListener girlWorkoutsListener = new View.OnClickListener() {
		public void onClick(View v) {

			// Create an array to specify the fields we want to display in the list (only TITLE)
			workoutList = new ArrayList<Workout>(controller.listGirlWorkouts());
		//	workout = controller.listGirlWorkouts();
			workoutList = controller.nameOrder(workoutList);
			fillData(workoutList);

		}
	};

	private View.OnClickListener heroWorkoutsListener = new View.OnClickListener() {	
		public void onClick(View v) {
			workoutList = new ArrayList<Workout>(controller.listHeroWorkouts());
			//workout = controller.listHeroWorkouts();
			workoutList = controller.nameOrder(workoutList);
			fillData(workoutList);

		}
	};


	private View.OnClickListener olympicWorkoutsListener = new View.OnClickListener() {	
		public void onClick(View v) {
			workoutList = new ArrayList<Workout>(controller.listSpecialWorkouts());
			//workout = controller.listSpecialWorkouts();
			workoutList = controller.nameOrder(workoutList);
			fillData(workoutList);

		}
	};

	private View.OnClickListener userWorkoutsListener = new View.OnClickListener() {	
		public void onClick(View v) {
			
	    	try {
	    		workoutList = new ArrayList<Workout>(controller.listUserWorkouts());
	    	} catch (Exception e){
	    		noUserWorkoutNotify();
	    		return;
	    	}
			
			workoutList = controller.nameOrder(workoutList);
			fillData(workoutList);

		}
	};
	
	private void noUserWorkoutNotify(){
		Toast.makeText(this, "No workouts have been added", Toast.LENGTH_LONG).show();
	}

	private void fillData(List<Workout> wkt){

		TableRow row;
		TextView t1;
		TextView t2;

		// Clear the table
		workout_title_table.removeAllViewsInLayout();
		row = new TableRow(this);
		workout_title_table.addView(row, new TableLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		workout_title_table.removeAllViewsInLayout();
		
		// Fill out the exercise history table
		for (int current = 0; current < wkt.size(); current++ ) {
			row = new TableRow(this);

			t1 = new TextView(this);
			t2 = new TextView(this);
			
			if ( wkt.get(current).getTitle().length() > 15 ){
				t1.setText(wkt.get(current).getTitle().substring(0,15) + " ");
			}
			else{ t1.setText(wkt.get(current).getTitle() ); }
			
			if ( wkt.get(current).getDescription().length() > 30 ){
				t2.setText(wkt.get(current).getDescription().substring(0,30));
			}
			else{ t2.setText(wkt.get(current).getDescription() ); }

			row.addView(t1);
			row.addView(t2);

			row.setId(current);

			workout_title_table.addView(row, new TableLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			row.setOnClickListener(new  OnClickListener() {
				// Row was selected
				public void onClick(View v) {
					v.setBackgroundColor(Color.DKGRAY);

					displayWorkoutDetails(v.getId());

				}
			});

		}

	}


	public void displayWorkoutDetails(int id){
		// TODO: Get the description, time, etc... from the selected exercise.
		final int delLocation = id;
		
		
		/** To hold workout details */
		String workoutDetails = String.format
		("%s\n\nWeight: %s\nReps: %s\nTime: %s\n\n%s", 
				workoutList.get(id).getDescription(), 
				workoutList.get(id).getWeight(), 
				workoutList.get(id).getReps(),
				( workoutList.get(id).getTime() == 0 ? "": workoutList.get(id).getTimeReadable() ),
				( workoutList.get(id).getNotes() == null ? "": workoutList.get(id).getNotes() )
				);

		// Build an exercise description window to display
		AlertDialog.Builder alertDialogBuilder = 
			new AlertDialog.Builder(this);

		alertDialogBuilder.setTitle(workoutList.get(id).getTitle());
		alertDialogBuilder.setIcon(me.croute.calendarexample.R.drawable.eft);

		alertDialogBuilder.setMessage(workoutDetails)
		.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				// if this button is clicked, just close
				// the dialog box and do nothing
				dialog.cancel();
			}
		});

		alertDialogBuilder.setMessage(workoutDetails)
		.setPositiveButton("Use This Workout",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				// if this button is clicked, fill the fields in
				// the addworkout page with the selected workout's information
				
				Workout w = workoutList.get(delLocation);
				
				Intent workoutUpdate = new Intent( getApplicationContext(), AddWorkout.class );
				
				workoutUpdate.putExtra(  "Title", w.getTitle());
				workoutUpdate.putExtra( "Description", w.getDescription());
				
				// add date that was passed in
				workoutUpdate.putExtra(  "Date", date );
				
				workoutUpdate.putExtra(  "Reps", w.getReps());	
				workoutUpdate.putExtra(  "Weight", w.getWeight());				
				workoutUpdate.putExtra(  "Time", w.getTime());	
				workoutUpdate.putExtra(  "Notes", w.getNotes());	
				
				startActivity(workoutUpdate);
			}
		});

		// create alert dialog
		AlertDialog alertDialog = alertDialogBuilder.create();

		// show it
		alertDialog.show();

	}

}
